'''
python -m pip install swanlab modelscope==1.22.0 "transformers>=4.50.0" datasets==3.2.0 accelerate pandas addict -i https://mirrors.aliyun.com/pypi/simple/

'''
import torch.distributed as dist
from rich import  print
print(dist.__file__)
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen3-0.6B"
model_folder = r"D:\models\qwen3-0.6b"

# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_folder)
model = AutoModelForCausalLM.from_pretrained(
    model_folder,
    torch_dtype="auto",
    device_map="auto"
)

# prompt = "Give me a short introduction to large language model."
prompt = "简短的介绍下自己"
messages = [
    {"role": "user", "content": prompt}
]

text = f"Instruction: {prompt}\nInput: {prompt}\nAnswer: "


# text = tokenizer.apply_chat_template(
#     messages,
#     tokenize=False,
#     add_generation_prompt=True,
#     enable_thinking=False # Switches between thinking and non-thinking modes. Default is True.
# )
# print(text)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
print(model_inputs)

generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=32768
)

# output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() 
output_ids = generated_ids[0].tolist() 
print("output_ids",output_ids)

try:
    # rindex finding 151668 (</think>)
    index = len(output_ids) - output_ids[::-1].index(151668) # [1,2,3,4,5,111,22,33 111 123 123 333]
except ValueError:
    index = 0    

thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
all = tokenizer.decode(output_ids, skip_special_tokens=True).strip("\n")

print("thinking content:", thinking_content)
print("content:", content)
print('\n')
print("all:", all)





